package com.kele.search.controller;

import com.kele.model.goods.SearchGoods;
import com.kele.search.feign.GoodsFeignClient;
import com.kele.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("search")
@CrossOrigin
public class SearchController {

    @Autowired
    HttpServletResponse response;

    @Autowired
    SearchService searchService;
    @Autowired
    GoodsFeignClient goodsFeignClient;

    @Value("${server.port}")
    String port;
    @GetMapping("test")
    public String test() throws IOException, ParseException {
        System.out.println("--->" + port);
        response.addHeader("Cache-Control","no-cache");
        response.addHeader("Cache-Control","no-store");

        return port;
    }
    @PostMapping("/{pageNo}/{pageSize}")
    public Map search(@PathVariable Integer pageNo,
                                          @PathVariable Integer pageSize,
                                          @RequestBody Map searchMap) throws IOException, ParseException {
        return searchService.search(pageNo,pageSize,searchMap);
    }

    @PostMapping("test/goods/findUpdatedAfter/{lastSyncTime}")
    public List<SearchGoods> findUpdatedAfter(@PathVariable Date lastSyncTime){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(lastSyncTime);
        return goodsFeignClient.findUpdatedAfter(format);
    }
}
